package com.aispeech.asr.apis.ssc.logger;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.aispeech.asr.apis.ssc.data.domain.TaskDoc;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class BigDataLogger {
    private final static String NODE_NAME = System.getenv().getOrDefault(
            "NODENAME",
            System.getenv().getOrDefault("HOSTNAME", "default")
    );
    private final static String ENV = System.getenv().getOrDefault("ENV", "dev");
    private final static int LOG_ID = 147;
    private final static String PROJECT = "lvpr";
    protected final static ObjectMapper objectMapper = new ObjectMapper();

    public static void logTask(TaskDoc task) {
        String taskId = task.getId();
        String productId = task.getProductId();

        Map<String, Object> request = task.getRequest();

        Map<String, Object> data = new HashMap<String, Object>(){{
            put("eventName", "TASK_SSC");
            put("logId", LOG_ID);
            put("env", ENV);
            put("project", PROJECT);
            put("module", "TASK_SSC");
            put("moduleVersion", "0.6.2");
            put("protoVersion", "0.6.2");
            put("logType", "json");
            put("timestamp", System.currentTimeMillis() / 1000);
            put("level", "INFO");
            put("instanceId", NODE_NAME);
            put("userId", "0");
            put("recordId", taskId);
            put("sessionId", taskId);
            put("productId", productId);
            put("audioUrl", String.format("/audios/task_%s/origin.%s", taskId, ((String)request.getOrDefault("audioType", "wav")).toLowerCase()));
            put("message", task);
        }};

        try {
            log.info(String.format("LOGBUSSTART|%s|LOGBUSEND", objectMapper.writeValueAsString(data)));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }
}
